package com.wsjzzcbq.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsjzzcbq.bean.Account;
import com.wsjzzcbq.mapper.AccountMapper;
import com.wsjzzcbq.service.AccountService;
import org.springframework.stereotype.Service;

/**
 * AccountServiceImpl
 *
 * @author wsjz
 * @date 2023/10/23
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Override
    public boolean deductAccount(String userId, int money, boolean rollback) {
        System.out.println("扣减");
        UpdateWrapper<Account> up = new UpdateWrapper<>();
        String sql = "money = money - " + money;
        up.setSql(sql);
        up.eq("user_id", userId);
        this.update(up);

        if (rollback) {
            int a = 1/0;
        }

        return true;
    }

    @Override
    public boolean compensateDeductAccount(String userId, int money) {
        System.out.println("补偿");
        UpdateWrapper<Account> up = new UpdateWrapper<>();
        String sql = "money = money + " + money;
        up.setSql(sql);
        up.eq("user_id", userId);
        this.update(up);
        return true;
    }
}
